import requests

# API的URL
url = "http://200.1.1.41:3001/v1/datasets/{dataset_id}/documents/{document_id}/update-by-file".format(
    dataset_id="8129c0ff-74f0-4b47-b609-d0a2cd7278b3",
    document_id="6ba6a067-03a5-45a4-8090-28d52fda2a05"
)

# 设置请求头
headers = {
    'Authorization': 'Bearer dataset-nS5zEK03kfDvmAqLl2jkP7pT'
}

# 设置表单数据
form_data = {
    'data': '{"indexing_technique":"high_quality","process_rule":{"mode":"automatic"}}'
}

# 设置文件数据
files = {
    'file': open(r'D:\workspace\wb-demo\crawler\crawler\laws\上海\上海市中小学校外实践教育促进规定(2024 08 22).txt', 'rb')
}

try:
    # 发送POST请求
    response = requests.post(url, headers=headers, data=form_data, files=files)

    # 检查响应状态码
    if response.status_code == 200:
        print("请求成功！")
        print("响应内容:", response.json())
    else:
        print("请求失败，状态码:", response.status_code)
        print("错误信息:", response.text)
except Exception as e:
    print("发生错误:", e)
finally:
    # 确保文件被关闭
    if 'file' in files:
        files['file'].close()